//
//  ParkingView.m
//  CarLife
//
//  Created by yu on 2017/11/6.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "NearSelectView.h"

#import "ParkingCell.h"

@implementation NearSelectView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        //self.backgroundColor = [UIColor whiteColor];
        self.layer.shadowRadius = 2;
        self.layer.shadowOpacity = 0.3;
        self.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    }
    return self;
}

- (void)updateInfoView:(SearchAnnotation *)info {
    self.titleLabel.text = info.title;
    PPLog(@"%ld-%@-%@",(long)info.distance,info.parkingType,info.address);
    NSString *subtitle;
    if (info.distance == 0 && info.parkingType == nil) {//00
        subtitle = info.address;
    } else if (info.distance == 0 && info.parkingType) {//01
        subtitle = NSStringFormat(@"%@ %@",info.parkingType,info.address);
    } else if (info.distance != 0 && info.parkingType == nil) {//10
        subtitle = NSStringFormat(@"%ldm %@",info.distance,info.address);
    } else {//11
        subtitle = NSStringFormat(@"%ldm %@ %@",info.distance,info.parkingType,info.address);
    }
    self.subTitleLabel.text = subtitle;
    
}
- (IBAction)handleAction:(UIButton *)btn {
    if (self.handleBlock) {
        self.handleBlock(btn.tag);
    }
}

- (void)showParkingView:(BOOL)show {
    if (show) {
        [UIView animateWithDuration:0.2 animations:^{
            CGRect frame = self.frame;
            frame.origin.y = kScreenHeight - 64 - 130;
            self.frame = frame;
        }];
    } else {
        [UIView animateWithDuration:0.2 animations:^{
            CGRect frame = self.frame;
            frame.origin.y = kScreenHeight - 64;
            self.frame = frame;
        }];
    }
}


@end



//**********************************************
@interface ParkingListView ()<UITableViewDelegate,UITableViewDataSource>

@end

static NSString *parkingCellIdentifier = @"parkingCellIdentifier";

@implementation ParkingListView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.shadowRadius = 2;
        self.layer.shadowOpacity = 0.3;
        self.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        
        [self setupTableView];
    }
    return self;
}

- (void)setupTableView
{
    _spreadBtn = [[UIButton alloc] init];
    if (@available(iOS 13.0, *)) {
        [_spreadBtn setBackgroundColor:UIColor.secondarySystemBackgroundColor];
    } else {
        [_spreadBtn setBackgroundColor:UIColor.whiteColor];
    }
    [_spreadBtn setTitle:@"点击展开更多结果" forState:UIControlStateNormal];
    [_spreadBtn setTitleColor:kTextBlackColor forState:UIControlStateNormal];
    _spreadBtn.titleLabel.font = SYSTEM_FONT(15);
    [_spreadBtn addTarget:self action:@selector(showMoreAction) forControlEvents:UIControlEventTouchUpInside];
    _spreadBtn.alpha = 0;
    _spreadBtn.hidden = YES;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.bounces = YES;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self addSubview:_spreadBtn];
    [self addSubview:_tableView];
    
    _spreadBtn.sd_layout
    .leftEqualToView(self)
    .rightEqualToView(self)
    .topEqualToView(self)
    .heightIs(45);
    
//    _tableView.sd_layout
//    .leftEqualToView(self)
//    .rightEqualToView(self)
//    .topSpaceToView(self, 0)
//    .bottomEqualToView(self);
    
    _tableView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
    [_tableView registerNib:NIB_NAMED(@"ParkingCell") forCellReuseIdentifier:parkingCellIdentifier];
}

- (void)showMoreAction {
    if (self.showMoreBlock) {
        self.showMoreBlock();
    }
}

- (void)setPois:(NSMutableArray *)pois
{
    _pois = pois;
    
    [_tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _pois.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ParkingCell *cell = [tableView dequeueReusableCellWithIdentifier:parkingCellIdentifier];
    SearchAnnotation *annotation = self.pois[indexPath.row];
    [cell setAnnotation:annotation];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchAnnotation *annotation = self.pois[indexPath.row];
//    PPLog(@"select = %@",annotation.title);
    if ([self.delegate respondsToSelector:@selector(selectedTableViewCellAction:)]) {
        [self.delegate selectedTableViewCellAction:annotation];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
//    PPLog(@"%lf",offsetY);
    if (offsetY < -25) {
        if (self.moveBlock) {
            self.moveBlock();
        }
    }
}

- (void)showParkingListView:(BOOL)show differ:(CGFloat)differ {
    if (show) {
        [UIView animateWithDuration:0.2 animations:^{
            self.hidden = NO;
            self.alpha = 1.0;
            
            CGRect frame = self.frame;
            frame.origin.y = kScreenHeight - 64 - differ;
            self.frame = frame;
            // 45为按钮的高度 隐藏table view 打开'展开更多'按钮
            if (differ == 45) {
                [self showTableView:NO];
                [self showSpreadBtn:YES];
            } else {
                [self showTableView:YES];
                [self showSpreadBtn:NO];
            }
        }];
    } else {
        [UIView animateWithDuration:0.2 animations:^{
            self.hidden = YES;
            self.alpha = 0;
            
            CGRect frame = self.frame;
            frame.origin.y = kScreenHeight - differ;
            self.frame = frame;
//            [self showTableView:NO];
        }];
    }
}

- (void)showSpreadBtn:(BOOL)show {
    if (show) {
        [UIView animateWithDuration:0.2 animations:^{
            self.spreadBtn.hidden = NO;
            self.spreadBtn.alpha = 1.0;
        }];
    } else {
        [UIView animateWithDuration:0.2 animations:^{
            self.spreadBtn.hidden = YES;
            self.spreadBtn.alpha = 0;
        }];
    }
}

- (void)showTableView:(BOOL)show {
    if (show) {
        [UIView animateWithDuration:0.2 animations:^{
            self.tableView.hidden = NO;
            self.tableView.alpha = 1.0;
        }];
    } else {
        [UIView animateWithDuration:0.2 animations:^{
            self.tableView.hidden = YES;
            self.tableView.alpha = 0;
        }];
    }
}


@end


